package com.ethan.modules.organization.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ethan.common.orm.Page;
import com.ethan.modules.organization.dao.UserDao;
import com.ethan.modules.organization.model.User;

@Component
@Service
@Transactional
public class UserService {
	@Resource
	private UserDao userDao;
	
	public Page<User> findPage(final Page<User> page, final String hql){
		return userDao.findPage(page, hql); 
	}
	
	public User findUniqueBy(final String propertyName,
			final Object value) {
		User account = userDao.findUniqueBy(propertyName, value);
		return account;
	}
	
	public List<User> find(String hql) {
		List<User> list = new ArrayList<User>();
		list = userDao.find(hql);
		return list;
	}

	public void save(User user) {
		userDao.save(user);
	}
	
	public User findID(Long id){
		return  userDao.get(id);
	}

	public List<User> findby(final String propertyName, final Object value) {
		List<User> list = new ArrayList<User>();
		list = userDao.findBy(propertyName, value);
		return list;
	}
	
	/** 
	 * 根据给定的用户名判断用户是否存在 
	 */
	public boolean userExists(String username) {
		List<User> list = userDao.findBy("loginName", username);
		if(list.isEmpty()){
			return false;
		}
		return true;
	}
	
	/** 
	 * 用户真实删除
	 */
	public void deleteUser(Long id) {
		try{
			User user = userDao.get(id);
			userDao.delete(user);
		}catch(Exception e){
			throw new IllegalArgumentException("发生异常,无法删除账号信息.");
		}
	}

	
}
